<?php
/**
 * (exporter)
 */
namespace App\Exporter\Stocking;

use App\Models\Inventory\PublicCn;
use App\Models\Product\ProductClassify;
use App\Models\SsBasic\AreaSku;
use App\Models\SsBasic\AreaSkuEstimate;
use App\Models\Stocking\StockingSku;
use App\Util\User;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class SkuExporter extends ExcelExporter implements WithMapping,WithHeadings
{

    protected $fileName = '备货产品.csv';

    public function headings(): array
    {
        $head1 = ['品类','站点','SKU','季节性','来源','销售比例','产品名称',
            '3天AVG','7天AVG','15天AVG','30天AVG','AVG',
            'FBA在库','FBA在途', '公共在库','国内在库','国内在途', '海外仓在库','海外仓在途',
            '生产周期','空运时效','空运最快可售', '海运时效','海运最快可售',
                  '空运发货安全系数','海运发货安全系数','发货目前系数','空运备货安全系数','海运备货安全系数','备货目前系数'];
        $head2 = [];
        for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
            $head2[] = date('y/m',strtotime("midnight first day of $i month")).'预估';
        }
        $head3 = ['备货推荐','推荐说明','可调剂库存','积压库存','积压货值','备货申请','备货原因','备货审批','审批原因','状态','备货员'];
        return array_merge($head1,$head2,$head3);
    }

    public function map($model) : array
    {
        $data1 = [
            $model->skuClassify->title ?? '',
            $model->area->area ?? '',
            $model->sku->psku_code ?? '',
            ProductClassify::$_season[$model->skuClassify->season ?? 3],
            isset($model->area_sku->type) && $model->area_sku->type ? AreaSku::$_type[$model->area_sku->type] : '',
            (isset($model->ratio->ratio) && $model->ratio->ratio > 0) ? ($model->ratio->ratio * 100) . '%' : '0%',
            $model->sku->psku_cn ?? '',
            $model->avg3 ?: 0,
            $model->avg7 ?: 0,
            $model->avg15 ?: 0,
            $model->avg30 ?: 0,
            $model->avg ?: 0,
            $model->fba_instant_num ?: 0,
            $model->fba_transit_num ?: 0,
            PublicCn::getInstant($model->psku_id),
            $model->cn_instant_num ?: 0,
            $model->cn_transit_num ?: 0,
            $model->os_instant_num ?: 0,
            $model->os_transit_num ?: 0,
            $model->purchase_cycle ?: 0,
            $model->air_day ?: 0,
            StockingSku::getSaleDate($model->air_available),
            $model->sea_day ?: 0,
            StockingSku::getSaleDate($model->sea_available),
            $model->air_ship_factor,
            $model->sea_ship_factor,
            StockingSku::getShipWithOSCurrentFactor($model).'('.StockingSku::getShipCurrentFactor($model).')',
            $model->air_stock_factor,
            $model->sea_stock_factor,
            StockingSku::getStockCurrentFactor($model)];
        $data2 = [];
        for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
            $data2[] = $model->{"estimate_num$i"} ?: 0;
        }
        $tj = StockingSku::getSystemTJ($model);
        $desc = [];
        foreach($tj['desc'] as $key => $_desc){
            $desc[] = $key.'-'.$_desc;
        }
        $data3 = [
            $tj['num'],
            implode(';',$desc),
            $tj['free'],
            $model->over_stock,
            $model->over_stock_value,
            $model->apply_num,
            $model->apply_remark,
            $model->approval_num,
            $model->approval_remark,
            StockingSku::$_status[$model->status],
            $model->seller_id ? User::getUsernameById($model->seller_id) : '',
        ];
        return array_merge($data1,$data2,$data3);
    }
}
